/**
 * 
 */
package com.javaking.clanteam.studentutils.courses;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.actionbarsherlock.app.SherlockFragment;
import com.javaking.clanteam.studentutils.R;
import com.javaking.clanteam.studentutils.sql.CourseHelper;
import com.javaking.clanteam.studentutils.utils.DatePair;
import com.javaking.clanteam.studentutils.utils.EditableListFragment;
import com.javaking.clanteam.studentutils.utils.OnItemsChangedListener;

/**
 * @author scott
 *
 */
public class AddCourseFragment extends SherlockFragment implements OnItemsChangedListener<DatePair>{
	
	EditableListFragment<DatePair> mTimesFrag;
//	List<DatePair> mTimes;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
//		mTimes = new ArrayList<DatePair>();
		
		View view = inflater.inflate(R.layout.fragment_addcourse, container, false);
		
		view.findViewById(R.id.addCourseButton).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View clicked) {
				View v = getView();
				CourseData.Builder builder = new CourseData.Builder();
				String courseTitle = ((EditText)v.findViewById(R.id.courseNameEdit)).getText().toString();
				if (courseTitle==null||courseTitle.equals("")) {
					new AlertDialog.Builder(getActivity())
						.setTitle("ERROR")
						.setMessage("You must specify a title for your course.")
						.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
							}
						})
						.show();
					
					return;
				}
				builder.setCourseTitle(courseTitle);
				builder.setTeacher(((EditText)v.findViewById(R.id.teacherEdit)).getText().toString());
				builder.setRoom(((EditText)v.findViewById(R.id.roomEdit)).getText().toString());
				
				for (DatePair pair : mTimesFrag.getItems()) {
					builder.addTime(pair);
				}
				
				CourseData course = builder.build();
				new CourseHelper(getActivity()).addCourse(course);
				getFragmentManager().popBackStack();
			}
		});
		return view;
	}
	
	@SuppressLint("NewApi")
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mTimesFrag = EditableListFragment.addToFrame(getActivity(), null,true,NewTimeDialog.class,false);
		
		if (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.HONEYCOMB) {
			ActionBar bar = getActivity().getActionBar();
			if (bar!=null) {
				bar.setTitle(R.string.addCourse);
			}
		}
	}

	@Override
	public void onItemsAdded(List<DatePair> items) {
//		mTimes.addAll(items);
	}

	@Override
	public void onItemRemoved(DatePair item) {
//		mTimes.remove(item);
	}

}
